package com.hymjweb.demo.hymj.system_maintenance.role_permission.controller;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.system_maintenance.role_permission.bean.RolePermissionPojo;
import com.hymjweb.demo.hymj.system_maintenance.role_permission.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hymjweb.demo.hymj.system_maintenance.role_permission.controller
 * @Author: luoge
 * @CreateTime: 2021-03-23 18:48
 * @Description:
 */

@Controller
@RequestMapping("/pages/system_maintain/role_permission")
public class RolePermissionController extends BaseController {

    @Autowired
    private RolePermissionService rolePermissionService;


    @RequestMapping("/rolePermissionInit")
    public String roleInit() {
        return "pages/system_maintain/role_permission/rolePermissionInit";
    }

    @RequestMapping("/queryRolePermissionPojoByRoleId")
    @ResponseBody
    public Object queryRolePermissionPojoByRoleId(String roleId) throws Exception {
        if (DataTypeUtil.isEmptyStr(roleId)) {
            throwException("获取角色对象参数错误!");
        }
        BaseResultMap baseResultMap = new BaseResultMap();

        List<RolePermissionPojo> rolePermissionPojoList = rolePermissionService.getRolePermissionListByRoleId(roleId);
        if (rolePermissionPojoList == null || rolePermissionPojoList.isEmpty()) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("角色ID参数错误!");
        } else {
            baseResultMap.setOk(true);
            baseResultMap.setData(rolePermissionPojoList);
        }
        return baseResultMap;
    }

    @RequestMapping("/roleMenuPermission")
    @ResponseBody
    public Object roleMenuPermission(@RequestBody JSONObject roleMenuPermissionJsonObj) throws Exception {
        if (roleMenuPermissionJsonObj == null || roleMenuPermissionJsonObj.isEmpty()) {
            throwException("角色菜单资源授权失败");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("角色菜单资源授权成功!");

        int affectRows = rolePermissionService.addRoleMenuPermission(roleMenuPermissionJsonObj);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("角色菜单资源授权失败!");
        }
        return baseResultMap;
    }

    @RequestMapping("/queryRolePermissionByRoleIdAndResourceTypeId")
    @ResponseBody
    public Object queryRolePermissionByRoleIdAndResourceTypeId(String roleId, Long resourceTypeId) throws Exception {
        if(DataTypeUtil.isEmptyStr(roleId)){
            throwException("角色ID参数错误");
        }

        if (resourceTypeId == null) {
            throwException("角色资源类型参数错误");
        }


        BaseResultMap baseResultMap = new BaseResultMap();

        List<RolePermissionPojo> permissionPojoList = rolePermissionService.queryRolePermissionByRoleIdAndResourceTypeId(roleId, resourceTypeId);

        if (permissionPojoList == null || permissionPojoList.isEmpty()) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("无对应的授权资源可用!");
        }
        else{
            baseResultMap.setOk(true);
            baseResultMap.setData(permissionPojoList);
        }

        return baseResultMap;
    }


    @RequestMapping("/saveRoleButtonPermission")
    @ResponseBody
    public Object saveRoleButtonPermission(@RequestBody JSONObject jsonObject) throws Exception {
        if (jsonObject == null || jsonObject.isEmpty()) {
            throwException("模块授权参错误!");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("模块授权保存成功!");

        int affectRows = rolePermissionService.saveRoleButtonPermission(jsonObject);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("授权保存失败,请重试!");
        }

        return baseResultMap;
    }


}
